package org.seven.jrdp.commons.util;

import java.util.Calendar;

import org.apache.commons.codec.binary.Base64;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

public abstract class TokenUtils {
	private static final byte[] SIGNING_KEY = Base64.decodeBase64(TokenUtils.class.getName());
	private static final SignatureAlgorithm SIGNATURE_ALGORITHM = SignatureAlgorithm.HS512;
	private static final int EXPIRY_TIME = 7200;

	public static String createToken(String sub) {
		Calendar calendar = DateTimeUtils.getCalendar();
		calendar.add(Calendar.SECOND, EXPIRY_TIME);
		return Jwts.builder().signWith(SIGNATURE_ALGORITHM, SIGNING_KEY).setExpiration(calendar.getTime()).setSubject(sub).compact();
	}

	public static String parseToken(String token) {
		try {
			return Jwts.parser().setSigningKey(SIGNING_KEY).parseClaimsJws(token).getBody().getSubject();
		} catch (Exception e) {
			return null;
		}
	}
}